package org.jeecg.modules.noRoadLedger.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.noRoadLedger.entity.NoRoadLedger;
import org.jeecg.modules.noRoadLedger.service.INoRoadLedgerService;
import org.jeecg.modules.noRoadUpload.entity.NoRoadUpload;
import org.jeecg.modules.noRoadUpload.service.INoRoadUploadService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 非道路移动机械电子台账
 * @Author: jeecg-boot
 * @Date:   2024-07-24
 * @Version: V1.0
 */
@Api(tags="非道路移动机械电子台账")
@RestController
@RequestMapping("/noRoadLedger/noRoadLedger")
@Slf4j
public class NoRoadLedgerController extends JeecgController<NoRoadLedger, INoRoadLedgerService> {
	@Autowired
	private INoRoadLedgerService noRoadLedgerService;
	@Autowired
	private INoRoadUploadService noRoadUploadService;
	
	/**
	 * 分页列表查询
	 *
	 * @param noRoadLedger
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-分页列表查询")
	@ApiOperation(value="非道路移动机械电子台账-分页列表查询", notes="非道路移动机械电子台账-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(NoRoadLedger noRoadLedger,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<NoRoadLedger> queryWrapper = QueryGenerator.initQueryWrapper(noRoadLedger, req.getParameterMap());
		Page<NoRoadLedger> page = new Page<NoRoadLedger>(pageNo, pageSize);
		IPage<NoRoadLedger> pageList = noRoadLedgerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "非道路移动机械电子台账-分页列表查询")
	 @ApiOperation(value="非道路移动机械电子台账-分页列表查询", notes="非道路移动机械电子台账-分页列表查询")
	 @GetMapping(value = "/m/list")
	 public Result<?> mQueryPageList(NoRoadLedger noRoadLedger,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<NoRoadLedger> queryWrapper = QueryGenerator.initQueryWrapper(noRoadLedger, req.getParameterMap());
		 queryWrapper.orderByDesc("create_time");
		 Page<NoRoadLedger> page = new Page<NoRoadLedger>(pageNo, pageSize);
		 IPage<NoRoadLedger> pageList = noRoadLedgerService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param noRoadLedger
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-添加")
	@ApiOperation(value="非道路移动机械电子台账-添加", notes="非道路移动机械电子台账-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody NoRoadLedger noRoadLedger) {
		noRoadLedgerService.save(noRoadLedger);
		return Result.OK("添加成功！");
	}

	 @AutoLog(value = "非道路移动机械电子台账-外部-添加")
	 @ApiOperation(value="非道路移动机械电子台账-外部-添加", notes="非道路移动机械电子台账-外部-添加")
	 @PostMapping(value = "/m/add")
	 public Result<?> mAdd(@RequestBody NoRoadLedger noRoadLedger) {
		 String registrationCode = noRoadLedger.getRegistrationCode();
		 Date entryTime = noRoadLedger.getEntryTime();
		 Date exitTime = noRoadLedger.getExitTime();
		 if (entryTime == null && exitTime != null) {
			 NoRoadLedger lastByRegistrationCode = noRoadLedgerService.getLastByRegistrationCode(registrationCode);
			 if (lastByRegistrationCode != null) {
				 lastByRegistrationCode.setExitTime(exitTime);
				 noRoadLedgerService.updateById(lastByRegistrationCode);
				 return Result.OK("添加成功！");
			 }
		 }
		 NoRoadUpload byRegistrationCode = noRoadUploadService.getByRegistrationCode(registrationCode);
		 if (byRegistrationCode == null) {
			 return Result.error("添加失败,车辆未登记");
		 }
		 BeanUtils.copyProperties(byRegistrationCode, noRoadLedger);
		 noRoadLedger.setId(null).setCreateTime(null).setCreateBy(null)
				 .setUpdateBy(null).setUpdateTime(null);
		 noRoadLedgerService.save(noRoadLedger);
		 return Result.OK("添加成功！");
	 }
	
	/**
	 *  编辑
	 *
	 * @param noRoadLedger
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-编辑")
	@ApiOperation(value="非道路移动机械电子台账-编辑", notes="非道路移动机械电子台账-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody NoRoadLedger noRoadLedger) {
		noRoadLedgerService.updateById(noRoadLedger);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-通过id删除")
	@ApiOperation(value="非道路移动机械电子台账-通过id删除", notes="非道路移动机械电子台账-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		noRoadLedgerService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-批量删除")
	@ApiOperation(value="非道路移动机械电子台账-批量删除", notes="非道路移动机械电子台账-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.noRoadLedgerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "非道路移动机械电子台账-通过id查询")
	@ApiOperation(value="非道路移动机械电子台账-通过id查询", notes="非道路移动机械电子台账-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		NoRoadLedger noRoadLedger = noRoadLedgerService.getById(id);
		if(noRoadLedger==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(noRoadLedger);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param noRoadLedger
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, NoRoadLedger noRoadLedger) {
        return super.exportXls(request, noRoadLedger, NoRoadLedger.class, "非道路移动机械电子台账");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, NoRoadLedger.class);
    }

}
